<?php
namespace app\admin\logic;
use app\admin\validate\RegionDict as RegionDictValidate;

class RegionDictLogic extends BaseLogic {

    /**
     * 获取区域字典查询条件
     * @return array
     */
    public function get_search_region_dict_maps(){
        $maps = [];
        return $maps;
    }

    /**
     * 检查新增区域字典规则
     * @param $params
     * @return mixed
     */
    public function check_add_region_dict_rule($params){
        $validate = new RegionDictValidate();
        $r = $validate->scene('add')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['name','=',trim($params['name'])];
        $rd_detail = model('region_dict')->get_detail($maps,'id');
        if(!empty($rd_detail)){
            return $this->get_response(0,'该区域已存在');
        }
        return $this->get_response(1,'OK');
    }

    /**
     * 检查修改区域字典规则
     * @param $params
     * @return mixed
     */
    public function check_save_region_dict_rule($params){
        $validate = new RegionDictValidate();
        $r = $validate->scene('edit')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['rdid']];
        $rd_detail = model('region_dict')->get_detail($maps,'id');
        if(empty($rd_detail)){
            return $this->get_response(0,'该区域不存在');
        }

        $maps = [];
        $maps[] = ['id','<>',$params['rdid']];
        $maps[] = ['name','=',trim($params['name'])];
        $detail = model('region_dict')->get_detail($maps,'id');
        if(!empty($detail)){
            return $this->get_response(0,'该区域已存在');
        }

        $data['rd_detail'] = $rd_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 获取区域字典公用查询条件
     * @return array
     */
    public function get_search_public_region_dict_maps(){
        $maps = [];
        return $maps;
    }
}